<?php

namespace app\admin\controller;

use addons\epay\library\Service;
use app\common\controller\Backend;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{

    /**
     * Order模型对象
     * @var \app\admin\model\Order
     */
    protected $model = null;
    protected $dataLimit = true;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Order;
        $this->view->assign("payTypeList", $this->model->getPayTypeList());
        $this->view->assign("payStatusList", $this->model->getPayStatusList());
        $this->view->assign("refundStatusList", $this->model->getRefundStatusList());
        $this->assignconfig('refund', $this->auth->check('order/refund')); //为了JS能获取到，同时判读权限 判断是否有major/chapter/index权限
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                    ->with(['admin','store'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);
            foreach ($list as $row) {
                $row->getRelation('admin')->visible(['nickname']);
				$row->getRelation('store')->visible(['name']);
            }

            $wechatMoney = $this->model->with(['admin','store'])->where($where)->where(['pay_type'=>'wechat'])->sum('price');
            $alipayMoney = $this->model->with(['admin','store'])->where($where)->where(['pay_type'=>'alipay'])->sum('price');
            $result = array("total" => $list->total(), "rows" => $list->items(), "wechatMoney" => $wechatMoney, "alipayMoney" => $alipayMoney);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function refund($ids =null){
        $row = $this->model->find($ids);
        if($row['update_time'] + 61 > time()){
            $this->error('请间隔一分钟后退款');
        }
        if($row['yue_money'] >= $row['price']){
            $this->error('可退款余额不足');
        }
        if ($this->request->isPost()) {
            $row = $this->model->find($ids);
            $rows =$this->request->post('row/a');
            $money =$rows['yue_money'];
            if($row['refund_status'] == 2 && $row['yue_money'] >= $row['price']){
                $this->error('订单已退款');
            }
            $params = [
                'order_sn' => $row['order_sn'],
                'amount' => $row['price'],
                'store_id' => $row['store_id'],
                'refund_money'=>$money
            ];
            $pay = Service::submitRefund($params,$row['pay_type']);
            if(($pay->code== '10000'&& $row['pay_type'] =='alipay') || $pay->return_code == 'SUCCESS'){
                $res = $this->model->where('id',$ids)->update([
                    'refund_status'=>2,
                    'refund_no'=> $row['pay_type'] == 'alipay' ? $pay->trade_no:$pay->out_refund_no,
                    'yue_money'=>$row['yue_money']+$money,
                    'update_time'=>time()]);
                return $this->success('退款成功');
            }else{
                return $this->error("退款失败:".$pay['msg']);
            }
        }else{
            $row['yue_money'] = $row['price'] - $row['yue_money'];
            $this->view->assign("row", $row);
            return $this->view->fetch();
        }
    }
}
